/*
 * miniFreeRTOS SOC System emulation.
 *
 * Copyright (c) 2024- yanl1229@163.com.
 * Written by yanl1229
 *
 * This code is licensed under the GPL.
 */
#ifndef LIST_H
#define LIST_H

#include <stddef.h>
#include <stdint.h>

/*
 * Definition of the only type of object that a list can contain.
 */
struct list_head;

/* 双向链表节点管理结构 */
struct list_node
{
    uint32_t value;                 /* 有序链表的键值 */
    struct list_node * next;        /* 后继节点 */
    struct list_node * prev;        /* 前驱节点 */
    void * priv;                    /* 保存私有数据 */
    struct list_head * list;        /* 节点所在的链表 */
};

struct list_head
{
    volatile unsigned long num;     /* 链表中元素的个数 */
    struct list_node *front;        /* 队列头哨兵(双链表的头结点) */
};

#define MAX_VALUE   0xffffffffUL

static uint32_t inline get_value_of_first_entry(struct list_head *list)
{
    return list->front->next->value;
}

/*
 * 链表是否为空
 */
static inline int list_is_empty(struct list_head *list)
{
    return (list->num == 0);
}

static inline void *get_owner_of_first_entry(struct list_head *list)
{
    return list->front->next->priv;
}

extern void list_init(struct list_head *list);


extern void list_add_order(struct list_head *list, struct list_node *entry);
extern void list_add_tail(struct list_head *list, struct list_node *entry);
extern unsigned long list_remove(struct list_node *entry);

#endif
